﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MultiWindows.Codes;
using System.IO;
using System.Collections;

namespace MultiWindows.Forms
{
    public partial class FormAnnouncement : Form
    {
        private List<string> announcement;
        public string path { get; set; }
        private StreamReader sr;
        private int i { get; set; }
        private int nCount { get; set; }

        public FormAnnouncement()
        {
            InitializeComponent();
        }

        private void FormAnnouncement_FormClosing(object sender, FormClosingEventArgs e)
        {
            ((FormMain)this.ParentForm).SaveWindowState(Window.ANNOUNCEMENT, this.Height, this.Width, this.Top, this.Left);
        }

        private void FormAnnouncement_Load(object sender, EventArgs e)
        {
            LoadAnnouncement();
        }

        public void LoadAnnouncement()
        {
            this.timer.Enabled = false;
            this.i = 0;
            this.txtAnnouncement.Text = "";
            sr = new StreamReader(this.path, Encoding.Default);
            this.announcement = new List<string>();

            while (!sr.EndOfStream)
            {
                this.announcement.Add(sr.ReadLine() + Environment.NewLine + Environment.NewLine);
            }
            this.nCount = this.announcement.Count;
            this.timer.Enabled = true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.txtAnnouncement.ScrollToCaret();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (i < nCount)
            {
                this.txtAnnouncement.AppendText(this.announcement[i]);
                i++;
            }
            else if (i < nCount + this.timer.Interval / 1000 - 1)
            {
                i++;
            }
            else 
            {
                this.txtAnnouncement.Text = "";
                i = 0;
            }
        }
    }
}
